﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace MobilnyDostawca.Model
{

    public class MobilnyDostawcaDataContext : DataContext
    {
        // Pass the connection string to the base class.
        public MobilnyDostawcaDataContext(string connectionString)
            : base(connectionString)
        { }

        public Table<Przesylka> Przesylki;
        public Table<Status> Statusy;
        public Table<Klient> Klienci;
        public Table<Region> Regiony;
        public Table<Subregion> Subregiony;
    }


    [Table]
    public class Przesylka : INotifyPropertyChanged, INotifyPropertyChanging
    {

        private int _id;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ID
        {
            get { return _id; }
            set
            {
                if (_id != value)
                {
                    NotifyPropertyChanging("ID");
                    _id = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }


        //private DateTime _timestamp;

        //[Column(DbType = "DATETIME", CanBeNull = true)]
        //public DateTime Timestamp
        //{
        //    get { return _timestamp; }
        //    set
        //    {
        //        if (_timestamp != value)
        //        {
        //            NotifyPropertyChanging("Timestamp");
        //            _timestamp = value;
        //            NotifyPropertyChanged("Timestamp");
        //        }
        //    }
        //}


        //private DateTime _dataReaktywacji;

        //[Column(DbType = "DATETIME", CanBeNull = true)]
        //public DateTime DataReaktywacji
        //{
        //    get { return _dataReaktywacji; }
        //    set
        //    {
        //        if (_dataReaktywacji != value)
        //        {
        //            NotifyPropertyChanging("DataReaktywacji");
        //            _dataReaktywacji = value;
        //            NotifyPropertyChanged("DataReaktywacji");
        //        }
        //    }
        //}


        private int _dostawcaId;

        [Column(DbType = "INT", CanBeNull = false)]
        public int Dostawca
        {
            get { return _dostawcaId; }
            set
            {
                if (_dostawcaId != value)
                {
                    NotifyPropertyChanging("Dostawca");
                    _dostawcaId = value;
                    NotifyPropertyChanged("Dostawca");
                }
            }
        }


        private int _magazynId;

        [Column(DbType = "INT", CanBeNull = false)]
        public int Magazyn
        {
            get { return _magazynId; }
            set
            {
                if (_magazynId != value)
                {
                    NotifyPropertyChanging("Magazyn");
                    _magazynId = value;
                    NotifyPropertyChanged("Magazyn");
                }
            }
        }


        [Column(DbType = "INT", CanBeNull = false)]
        internal int _nadawcaId;

        private EntityRef<Klient> _nadawca;

        [Association(Storage = "_nadawca", ThisKey = "_nadawcaId", OtherKey = "ID", IsForeignKey = true)]
        public Klient Nadawca
        {
            get { return _nadawca.Entity; }
            set
            {
                NotifyPropertyChanging("Nadawca");
                _nadawca.Entity = value;

                if (value != null)
                {
                    _nadawcaId = value.ID;
                }

                NotifyPropertyChanging("Nadawca");
            }
        }


        [Column(DbType = "INT", CanBeNull = false)]
        internal int _odbiorcaId;

        private EntityRef<Klient> _odbiorca;

        [Association(Storage = "_odbiorca", ThisKey = "_odbiorcaId", OtherKey = "ID", IsForeignKey = true)]
        public Klient Odbiorca
        {
            get { return _odbiorca.Entity; }
            set
            {
                NotifyPropertyChanging("Odbiorca");
                _odbiorca.Entity = value;

                if (value != null)
                {
                    _odbiorcaId = value.ID;
                }

                NotifyPropertyChanging("Odbiorca");
            }
        }


        [Column(DbType = "INT", CanBeNull = false)]
        internal int _statusId;

        private EntityRef<Status> _status;

        [Association(Storage = "_status", ThisKey = "_statusId", OtherKey = "ID", IsForeignKey = true)]
        public Status Status
        {
            get { return _status.Entity; }
            set
            {
                NotifyPropertyChanging("Status");
                _status.Entity = value;

                if (value != null)
                {
                    _statusId = value.ID;
                }

                NotifyPropertyChanging("Status");
            }
        }



        public string Nazwa
        {
            get { return "przesyłka " + ID; }
        }


        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

    [Table]
    public class Status : INotifyPropertyChanged, INotifyPropertyChanging
    {

        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int ID
        {
            get { return _id; }
            set
            {
                NotifyPropertyChanging("ID");
                _id = value;
                NotifyPropertyChanged("ID");
            }
        }

        // Define nazwa
        private string _nazwa;

        [Column(DbType = "NVARCHAR(32) NOT NULL")]
        public string Nazwa
        {
            get { return _nazwa; }
            set
            {
                NotifyPropertyChanging("Nazwa");
                _nazwa = value;
                NotifyPropertyChanged("Nazwa");
            }
        }

          // Define the entity set for the collection side of the relationship.
        private EntitySet<Przesylka> _przesylki;

        [Association(Storage = "_przesylki", OtherKey = "_statusId", ThisKey = "ID")]
        public EntitySet<Przesylka> Przesylki
        {
            get { return this._przesylki; }
            set { this._przesylki.Assign(value); }
        }


        // Assign handlers for the add and remove operations, respectively.
        public Status()
        {
            _przesylki = new EntitySet<Przesylka>(
                new Action<Przesylka>(this.attach_Przesylka),
                new Action<Przesylka>(this.detach_Przesylka)
                );
        }

        // Called during an add operation
        private void attach_Przesylka(Przesylka przesylka)
        {
            NotifyPropertyChanging("Przesylka");
            przesylka.Status = this;
        }

        // Called during a remove operation
        private void detach_Przesylka(Przesylka przesylka)
        {
            NotifyPropertyChanging("Przesylka");
            przesylka.Status = null;
        }


        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

    [Table]
    public class Klient : INotifyPropertyChanged, INotifyPropertyChanging
    {

        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, CanBeNull = false, IsPrimaryKey = true)]
        public int ID
        {
            get { return _id; }
            set
            {
                NotifyPropertyChanging("ID");
                _id = value;
                NotifyPropertyChanged("ID");
            }
        }


        private string _imie;

        [Column(DbType = "NVARCHAR(32) NOT NULL")]
        public string Imie
        {
            get { return _imie; }
            set
            {
                NotifyPropertyChanging("Imie");
                _imie = value;
                NotifyPropertyChanged("Imie");
            }
        }


        private string _nazwisko;

        [Column(DbType = "NVARCHAR(32) NOT NULL")]
        public string Nazwisko
        {
            get { return _nazwisko; }
            set
            {
                NotifyPropertyChanging("Nazwisko");
                _nazwisko = value;
                NotifyPropertyChanged("Nazwisko");
            }
        }


        private string _adres;

        [Column(DbType = "NVARCHAR(64) NOT NULL")]
        public string Adres
        {
            get { return _adres; }
            set
            {
                NotifyPropertyChanging("Adres");
                _adres = value;
                NotifyPropertyChanged("Adres");
            }
        }


        private string _telefon;

        [Column(DbType = "NVARCHAR(32) NOT NULL")]
        public string Telefon
        {
            get { return _telefon; }
            set
            {
                NotifyPropertyChanging("Telefon");
                _telefon = value;
                NotifyPropertyChanged("Telefon");
            }
        }

        [Column(DbType = "INT", CanBeNull = false)]
        internal int _subregionId;

        private EntityRef<Subregion> _subregion;

        [Association(Storage = "_subregion", ThisKey = "_subregionId", OtherKey = "ID", IsForeignKey = true)]
        public Subregion Subregion
        {
            get { return _subregion.Entity; }
            set
            {
                NotifyPropertyChanging("Subregion");
                _subregion.Entity = value;

                if (value != null)
                {
                    _subregionId = value.ID;
                }

                NotifyPropertyChanging("Subregion");
            }
        }


        // Define the entity set for the collection side of the relationship.
        private EntitySet<Przesylka> _przesylki;

        [Association(Storage = "_przesylki", OtherKey = "_odbiorcaId", ThisKey = "ID")]
        public EntitySet<Przesylka> Przesylki
        {
            get { return this._przesylki; }
            set { this._przesylki.Assign(value); }
        }


        // Assign handlers for the add and remove operations, respectively.
        public Klient()
        {
            _przesylki = new EntitySet<Przesylka>(
                new Action<Przesylka>(this.attach_Przesylka),
                new Action<Przesylka>(this.detach_Przesylka)
                );
        }

        // Called during an add operation
        private void attach_Przesylka(Przesylka przesylka)
        {
            NotifyPropertyChanging("Przesylka");
            przesylka.Odbiorca = this;
        }

        // Called during a remove operation
        private void detach_Przesylka(Przesylka przesylka)
        {
            NotifyPropertyChanging("Przesylka");
            przesylka.Odbiorca = null;
        }


        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

    [Table]
    public class Subregion : INotifyPropertyChanged, INotifyPropertyChanging
    {

        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int ID
        {
            get { return _id; }
            set
            {
                NotifyPropertyChanging("ID");
                _id = value;
                NotifyPropertyChanged("ID");
            }
        }


        private string _nazwa;

        [Column(DbType = "NVARCHAR(32) NOT NULL")]
        public string Nazwa
        {
            get { return _nazwa; }
            set
            {
                NotifyPropertyChanging("Nazwa");
                _nazwa = value;
                NotifyPropertyChanged("Nazwa");
            }
        }


        private int _dostawcaId;

        [Column(DbType = "INT", CanBeNull = false)]
        public int Dostawca
        {
            get { return _dostawcaId; }
            set
            {
                if (_dostawcaId != value)
                {
                    NotifyPropertyChanging("Dostawca");
                    _dostawcaId = value;
                    NotifyPropertyChanged("Dostawca");
                }
            }
        }


        [Column(DbType = "INT", CanBeNull = false)]
        internal int _regionId;

        private EntityRef<Region> _region;

        [Association(Storage = "_region", ThisKey = "_regionId", OtherKey = "ID", IsForeignKey = true)]
        public Region Region
        {
            get { return _region.Entity; }
            set
            {
                NotifyPropertyChanging("Region");
                _region.Entity = value;

                if (value != null)
                {
                    _regionId = value.ID;
                }

                NotifyPropertyChanging("Region");
            }
        }


        // Define the entity set for the collection side of the relationship.
        private EntitySet<Klient> _klienci;

        [Association(Storage = "_klienci", OtherKey = "_subregionId", ThisKey = "ID")]
        public EntitySet<Klient> Klienci
        {
            get { return this._klienci; }
            set { this._klienci.Assign(value); }
        }


        // Assign handlers for the add and remove operations, respectively.
        public Subregion()
        {
            _klienci = new EntitySet<Klient>(
                new Action<Klient>(this.attach_Klient),
                new Action<Klient>(this.detach_Klient)
                );
        }

        // Called during an add operation
        private void attach_Klient(Klient klient)
        {
            NotifyPropertyChanging("Klient");
            klient.Subregion = this;
        }

        // Called during a remove operation
        private void detach_Klient(Klient klient)
        {
            NotifyPropertyChanging("Klient");
            klient.Subregion = null;
        }


        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }

    [Table]
    public class Region : INotifyPropertyChanged, INotifyPropertyChanging
    {

        private int _id;

        [Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
        public int ID
        {
            get { return _id; }
            set
            {
                NotifyPropertyChanging("ID");
                _id = value;
                NotifyPropertyChanged("ID");
            }
        }


        private string _nazwa;

        [Column(DbType = "NVARCHAR(32) NOT NULL")]
        public string Nazwa
        {
            get { return _nazwa; }
            set
            {
                NotifyPropertyChanging("Nazwa");
                _nazwa = value;
                NotifyPropertyChanged("Nazwa");
            }
        }


        // Define the entity set for the collection side of the relationship.
        private EntitySet<Subregion> _subregiony;

        [Association(Storage = "_subregiony", OtherKey = "_regionId", ThisKey = "ID")]
        public EntitySet<Subregion> Subregiony
        {
            get { return this._subregiony; }
            set { this._subregiony.Assign(value); }
        }


        // Assign handlers for the add and remove operations, respectively.
        public Region()
        {
            _subregiony = new EntitySet<Subregion>(
                new Action<Subregion>(this.attach_Region),
                new Action<Subregion>(this.detach_Region)
                );
        }

        // Called during an add operation
        private void attach_Region(Subregion subregion)
        {
            NotifyPropertyChanging("Subregion");
            subregion.Region = this;
        }

        // Called during a remove operation
        private void detach_Region(Subregion subregion)
        {
            NotifyPropertyChanging("Subregion");
            subregion.Region = null;
        }


        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }
}